<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="配送单号" style="width:20%;" placeholder="请输入配送单号">
        <i slot="prefix" class="el-input__icon el-icon-search"/>
      </el-input>
      <el-date-picker
        v-model="日期"
        type="daterange"
        range-separator="至"
        value-format="yyyy-MM-dd"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <el-button type="primary" plain @click="Search">搜索</el-button>
      <el-button type="primary" plain @click="exportExcel()">导出</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;">
      <el-table-column label="配送单号" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.配送单号 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="商品信息 品名/货号/规格"
        min-width="250">
        <template slot-scope="scope">
          <span>{{ scope.row['品名'] }}/{{ scope.row['货号'] }}/{{ scope.row['包装规格'] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="时间" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.配送单生成时间 }}</span>
        </template>
      </el-table-column>

      <el-table-column label="订单状态" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.供货商查询显示 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课题卡号" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.课题卡号 }}</span>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        v-show="total>0"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { fetchList } from '@/api/api'

export default {
  data() {
    return {
      配送单号: '',
      日期: '',
      listLoading: false,
      list: [],
      total: 0,
      currentPage: 1,
      pageSize: 20,
      pageSizes: [20, 30, 50, 100]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    exportExcel() {
      if (this.日期 === null) this.日期 = ''
      const param = {
        query: 'vue供货商配送单_查询',
        供货商编号: localStorage.ghusername,
        配送单号: this.配送单号,
        起始日期: this.日期.length === 0 ? '' : this.日期[0],
        截止日期: this.日期.length === 0 ? '' : this.日期[1]
      }
      fetchList(param).then(({ data }) => {
        console.log(data)
          import('@/vendor/Export2Excel').then(excel => {
            const tHeader = ['商品名称', '品牌', '单价', '数量', '总价', '订单提交时间', '订单号', '配送单号', '收货人', '收货人地址', '收货人电话', '课题组名称', '请购人姓名']
            const filterVal = ['品名', '品牌', '成交价', '数量', '总价', '订单日期', '订单号', '配送单号', '收货人', '收货人地址', '收货人电话', '课题组名称', '请购人姓名']

            const d = this.formatJson(filterVal, data.data)
            excel.export_json_to_excel({
              header: tHeader,
              data: d,
              filename: '统计表格',
              autoWidth: true,
              bookType: 'xlsx'
            })
          })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    getList() {
      if (this.日期 === null) this.日期 = ''
      const param = {
        query: 'vue供货商配送单_查询',
        供货商编号: localStorage.ghusername,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        配送单号: this.配送单号,
        起始日期: this.日期.length === 0 ? '' : this.日期[0],
        截止日期: this.日期.length === 0 ? '' : this.日期[1]
      }
      fetchList(param).then(({ data }) => {
        if (data.flag === '1') {
          this.list = data.data
          this.total = parseInt(data.data[0].total)
        } else {
          this.total = 0
          this.list = []
        }
        this.listLoading = false
      })
    },
    Search() {
      this.getList()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    }
  }
}
</script>

<style>
  .app-container {
    margin-left: 1vw;
    margin-right: 1vw;
  }
</style>
